@tool
extends Control

signal remove_modifier(modifier:Modifier)

var modifier:Modifier
var attribute_dictionary:AttributeDictionary

func _ready() -> void:
	
	setAttributeToModifyOption()
	setModifierOptions()
	setMagnitudeCalculationOptions()
	
	if modifier:
		%Value.value = modifier.value


func setAttributeToModifyOption():
	%AttributeToModifyOption.clear()
	if attribute_dictionary:
		for i in attribute_dictionary.attirbute_list:
			%AttributeToModifyOption.add_item(i.name)
		
		var a = attribute_dictionary.attirbute_list.filter(func (x):
			return x.name == modifier.attribute_to_modify)
		var idx = attribute_dictionary.attirbute_list.find(a[0])
		%AttributeToModifyOption.select(idx)


func setModifierOptions():
	%ModifierOpOption.clear()
	if modifier:
		for i in modifier.ModifierOp:
			%ModifierOpOption.add_item(i)
		%ModifierOpOption.select(modifier.modifier_option)


func setMagnitudeCalculationOptions():
	%MagnitudeCalculationOption.clear()
	if modifier:
		for i in modifier.MagnitudeCalculation:
			%MagnitudeCalculationOption.add_item(i)
		%MagnitudeCalculationOption.select(modifier.magnitude_calculation)



func _on_attribute_to_modify_option_item_selected(index: int) -> void:
	var selected_attribute_name = attribute_dictionary.attirbute_list[index].name
	modifier.attribute_to_modify = selected_attribute_name


func _on_modifier_op_option_item_selected(index: int) -> void:
	modifier.modifier_option = index


func _on_magnitude_calculation_option_item_selected(index: int) -> void:
	modifier.magnitude_calculation = index


func _on_remove_pressed() -> void:
	remove_modifier.emit(modifier)


func _on_value_value_changed(value: float) -> void:
	modifier.value = value
